/*
 * @Author: 计都 3038199036@qq.com
 * @Date: 2024-09-20 14:45:16
 * @LastEditors: 计都 3038199036@qq.com
 * @LastEditTime: 2024-09-21 15:14:28
 * @FilePath: \express\routes\login.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const express = require('express');
const router = express.Router();
const sql = require("../db/index")

router.post('/login', async (req, res) => {
    // 获取前端传递的用户名和密码
    const { username, password } = req.body;
    // 查询数据库
    const [rows] = await sql.execute('SELECT * FROM order_user WHERE username =? AND password =?', [username, password]);
    // console.log(express.response,'-----');
    
    // 判断查询结果
    if (rows.length > 0) {
        //数据库中存在改用户且密码正确
        res.send({
            code: 200,
            message: '登录成功',
            data: rows[0],
            token: Math.random().toString(36)
        })
    } else {
        res.status(401).send('登录失败,用户名或密码错误');
    }
})
// 导出路由
module.exports = router